package com.dbManage.page.login;

import com.dbManage.entity.Message;
import com.dbManage.entity.User;
import com.dbManage.exception.CheckCodeException;
import com.dbManage.exception.UsernameException;
import com.dbManage.tools.SessionTool;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 登陆
 *
 * @author WangDong
 * @date 2018/1/4 10:55
 */
@Controller
public class LoginController {
    @Resource
    LoginService loginService;

    /**
     * 访问登陆
     *
     * @return
     */
    @RequestMapping(value = "/login")
    public String login() {
        return "login";
    }
    @RequestMapping("/loginCheck")
    @ResponseBody
    public Message loginCheck(User user, String code){
        try {
            loginService.loginCheck(user,code);
        } catch (CheckCodeException e) {
            return new Message(Message.STATUS_FAILED, e.getMessage());
        } catch (UsernameException e) {
            return new Message(Message.STATUS_FAILED, e.getMessage());
        }
        return new Message(Message.STATUS_SUCCESS, "登录成功");
    }
    /**
     * 注册页面
     *
     * @return
     */
    @RequestMapping("/registerView")
    public String registerView() {
        return "register";
    }

    /**
     * 注册
     *
     * @param user
     * @param checkCode
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public Message register(User user, String checkCode) {
        try {
            loginService.register(user, checkCode);
        } catch (CheckCodeException e) {
            return new Message(Message.STATUS_FAILED, e.getMessage());
        } catch (UsernameException e) {
            return new Message(Message.STATUS_FAILED, e.getMessage());
        }
        return new Message(Message.STATUS_SUCCESS, "注册成功");
    }

    /**
     * 重置密码页面
     *
     * @param type 1.修改密码页面  2.找回密码页面  ,该值用于判断返回按钮返回的页面
     * @return
     */
    @RequestMapping("/passReset")
    public String passReset(Model model, int id, @RequestParam(required = false, defaultValue = "1") int type) {
        model.addAttribute("id", id);
        model.addAttribute("type", type);
        return "passReset";
    }

    /**
     * 注销
     * @return
     */
    @RequestMapping("/logout")
    public String logout(){
      SessionTool.removeUser();
      return "redirect:/login";
    }

    /**
     * 获取验证码
     *
     * @param resp
     * @param session
     */
    @RequestMapping("/getCheckCode")
    public void getCode(HttpServletResponse resp, HttpSession session) {
        try {
            BufferedImage image = loginService.getImage(session);
            resp.setContentType("image/jpeg");
            OutputStream ops = resp.getOutputStream();
            ImageIO.write(image, "jpeg", ops);
            ops.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
